﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Temp.Properties;

namespace YArchitech.Temp
{
	public partial class DuctSpecificationForm : System.Windows.Forms.Form
	{
		public DuctSpecificationForm()
		{
			this.InitializeComponent();
		}

		public DuctSpecificationForm(HYDuctSpecifications roundSpecs, HYDuctSpecifications rectSpecs, bool IsReadOnly = false)
		{
			this.InitializeComponent();
			this.m_Rounds = roundSpecs;
			this.m_Rects = rectSpecs;
			if (IsReadOnly)
			{
				this.btnRectAdd.Visible = false;
				this.btnRectDel.Visible = false;
				this.btnRoundAdd.Visible = false;
				this.btnRoundDel.Visible = false;
				this.hyGridViewRect.EditMode = DataGridViewEditMode.EditProgrammatically;
				this.hyGridViewRound.EditMode = DataGridViewEditMode.EditProgrammatically;
			}
		}

		private void DuctSpecificationForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			base.CenterToParent();
			this.btnRectAdd.Image = YArchitech.LIB.Resources.AddNew16;
			this.btnRoundAdd.Image = YArchitech.LIB.Resources.AddNew16;
			this.btnRectDel.Image = YArchitech.LIB.Resources.delete16;
			this.btnRoundDel.Image = YArchitech.LIB.Resources.delete16;
			this.toolTip.SetToolTip(this.btnRoundAdd, "添加圆形风管规格");
			this.toolTip.SetToolTip(this.btnRoundDel, "删除选中规格");
			this.toolTip.SetToolTip(this.btnRectAdd, "添加矩形风管规格");
			this.toolTip.SetToolTip(this.btnRectDel, "删除选中规格");
			this.hyGridViewRect.Columns.Add(this.colWidth);
			this.hyGridViewRect.Columns.Add(this.colHeight);
			this.hyGridViewRound.Columns.Add(this.colDiameter);
			this.FillData(this.hyGridViewRect, this.m_Rects, true);
			this.FillData(this.hyGridViewRound, this.m_Rounds, false);
		}

		private void FillData(HYGridView gv, HYDuctSpecifications specs, bool IsRect = true)
		{
			if (IsRect)
			{
				using (SortedSet<HYDuctSpecification>.Enumerator enumerator = specs.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						HYDuctSpecification hyductSpecification = enumerator.Current;
						DataGridViewRow dataGridViewRow = new DataGridViewRow();
						dataGridViewRow.CreateCells(gv);
						this.SetCellValue(dataGridViewRow.Cells[0], hyductSpecification.Width);
						this.SetCellValue(dataGridViewRow.Cells[1], hyductSpecification.Height);
						gv.Rows.Add(dataGridViewRow);
					}
					return;
				}
			}
			foreach (HYDuctSpecification hyductSpecification2 in specs)
			{
				DataGridViewRow dataGridViewRow2 = new DataGridViewRow();
				dataGridViewRow2.CreateCells(gv);
				this.SetCellValue(dataGridViewRow2.Cells[0], hyductSpecification2.Diameter);
				gv.Rows.Add(dataGridViewRow2);
			}
		}

		private void SetCellValue(DataGridViewCell cell, int value)
		{
			cell.ValueType = typeof(int);
			cell.Value = value;
		}

		private int GetCellValue(DataGridViewCell cell)
		{
			if (cell.ValueType != typeof(int))
			{
				return 0;
			}
			return Convert.ToInt32(cell.Value);
		}

		private bool IsCellValueValid(DataGridViewCell cell)
		{
			return this.GetCellValue(cell) > 0;
		}

		private bool IsRowValid(DataGridViewRow row, bool IsRect = true)
		{
			bool flag = true;
			if (IsRect)
			{
				flag = this.IsCellValueValid(row.Cells[1]);
			}
			return flag && this.IsCellValueValid(row.Cells[0]);
		}

		private bool IsLastRowValid(HYGridView gv, bool IsRect = true)
		{
			int index = gv.Rows.Count - 1;
			DataGridViewRow row = gv.Rows[index];
			return this.IsRowValid(row, IsRect);
		}

		private void btnRoundAdd_Click(object sender, EventArgs e)
		{
			int index = this.hyGridViewRound.Rows.Count - 1;
			index = this.hyGridViewRound.AddNewRow();
			DataGridViewRow dataGridViewRow = this.hyGridViewRound.Rows[index];
			this.SetCellValue(dataGridViewRow.Cells[0], 0);
			this.hyGridViewRound.SelectRow(dataGridViewRow);
		}

		private void btnRoundDel_Click(object sender, EventArgs e)
		{
			if (this.hyGridViewRound.RowCount <= 1)
			{
				YJKMessageBox.Show("风管规格不能全部删除！");
				return;
			}
			if (DialogResult.Yes == MessageBox.Show(YArchitech.Temp.Properties.Resources.ConfirmForDelete, YArchitech.LIB.Resources.MessageBoxTitle, MessageBoxButtons.YesNo))
			{
				this.hyGridViewRound.DeleteSelectedRows();
			}
		}

		private void btnRectAdd_Click(object sender, EventArgs e)
		{
			int index = this.hyGridViewRect.Rows.Count - 1;
			index = this.hyGridViewRect.AddNewRow();
			DataGridViewRow dataGridViewRow = this.hyGridViewRect.Rows[index];
			this.SetCellValue(dataGridViewRow.Cells[0], 0);
			this.SetCellValue(dataGridViewRow.Cells[1], 0);
			this.hyGridViewRect.SelectRow(dataGridViewRow);
		}

		private void btnRectDel_Click(object sender, EventArgs e)
		{
			if (this.hyGridViewRect.RowCount <= 1)
			{
				YJKMessageBox.Show("风管规格不能全部删除！");
				return;
			}
			if (DialogResult.Yes == MessageBox.Show(YArchitech.Temp.Properties.Resources.ConfirmForDelete, YArchitech.LIB.Resources.MessageBoxTitle, MessageBoxButtons.YesNo))
			{
				this.hyGridViewRect.DeleteSelectedRows();
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			return base.ProcessCmdKey(ref msg, keyData);
		}

		private void DuctSpecificationForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (Convert.ToInt32(e.KeyChar) == 27)
			{
				base.DialogResult = DialogResult.Cancel;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.SaveData(this.hyGridViewRound, this.m_Rounds, false);
			this.SaveData(this.hyGridViewRect, this.m_Rects, true);
			HYDataAccess.SaveDuctSpecifications(this.m_Rounds, this.m_Rects);
			base.DialogResult = DialogResult.OK;
		}

		private void SaveData(HYGridView gv, HYDuctSpecifications specs, bool IsRect)
		{
			specs.Clear();
			HYShapeProfile shapeProfile = IsRect ? HYShapeProfile.Rectangular : HYShapeProfile.Round;
			foreach (object obj in ((IEnumerable)gv.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				DataGridViewCell dataGridViewCell = dataGridViewRow.Cells[0];
				int num = (int)dataGridViewRow.Cells[0].Value;
				HYDuctSpecification hyductSpecification = new HYDuctSpecification(shapeProfile, 800, 320);
				hyductSpecification.Width = this.GetCellValue(dataGridViewRow.Cells[0]);
				if (IsRect)
				{
					int num2 = (int)dataGridViewRow.Cells[1].Value;
					hyductSpecification.Height = this.GetCellValue(dataGridViewRow.Cells[1]);
				}
				if (hyductSpecification.IsValid)
				{
					specs.Add(hyductSpecification);
				}
			}
		}

		private void hyGridViewRect_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			int rowIndex = e.RowIndex;
			int columnIndex = e.ColumnIndex;
			if (rowIndex < 0 || columnIndex < 0)
			{
				return;
			}
			if (e.FormattedValue == null)
			{
				YJKMessageBox.Show("不能为空！");
				e.Cancel = true;
			}
			if (!Regex.IsMatch(e.FormattedValue.ToString(), "^[0-9]*[1-9][0-9]*$") || e.FormattedValue.ToString() == "0")
			{
				YJKMessageBox.Show("填入数字应为正整数！");
				e.Cancel = true;
			}
		}

		private void hyGridViewRound_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			int rowIndex = e.RowIndex;
			int columnIndex = e.ColumnIndex;
			if (rowIndex < 0 || columnIndex < 0)
			{
				return;
			}
			if (e.FormattedValue == null)
			{
				YJKMessageBox.Show("不能为空！");
				e.Cancel = true;
			}
			if (!Regex.IsMatch(e.FormattedValue.ToString(), "^[0-9]*[1-9][0-9]*$") || e.FormattedValue.ToString() == "0")
			{
				YJKMessageBox.Show("填入数字应为正整数！");
				e.Cancel = true;
			}
		}

		private void btnRoundDel_MouseEnter(object sender, EventArgs e)
		{
			this.beCheck = true;
		}

		private void btnRoundDel_MouseLeave(object sender, EventArgs e)
		{
			this.beCheck = false;
		}

		private HYDuctSpecifications m_Rounds;

		private HYDuctSpecifications m_Rects;

		private bool beCheck;

		public static string Header_Width = YArchitech.Temp.Properties.Resources.Width + YArchitech.Temp.Properties.Resources.MMUnitWithParenthesis;

		public static string Header_Height = YArchitech.Temp.Properties.Resources.Height + YArchitech.Temp.Properties.Resources.MMUnitWithParenthesis;

		public static string Diameter_Height = YArchitech.Temp.Properties.Resources.Diameter + YArchitech.Temp.Properties.Resources.MMUnitWithParenthesis;

		private HYDuctSpecificationColumn colWidth = new HYDuctSpecificationColumn(DuctSpecificationForm.Header_Width, 80);

		private HYDuctSpecificationColumn colHeight = new HYDuctSpecificationColumn(DuctSpecificationForm.Header_Height, 80);

		private HYDuctSpecificationColumn colDiameter = new HYDuctSpecificationColumn(DuctSpecificationForm.Diameter_Height, 120);
	}
}
